package com.ctg.sysmgr.sso.client.control.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.http.client.CookieStore;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.util.CommonUtils;

public class RemoteLoginStatusFilter extends AbstractCasFilter{
	
	private String callBackUrl;
	
	public void init(){
		super.init();
		
		CommonUtils.assertNotNull(this.callBackUrl, "callBackUrl cannot be null.");
	}
	
	protected void initInternal(final FilterConfig filterConfig) throws ServletException {
        if (!isIgnoreInitConfiguration()) {
            super.initInternal(filterConfig);
            setCallBackUrl(getPropertyFromInitParams(filterConfig, "callBackUrl", null));
        }
	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		String callBackUrl = getCallBackUrl(request, response);
		
		String ret = request.getParameter("ret")==null?"0":request.getParameter("ret");
		String msg = request.getParameter("msg");
		
		String result = "<html><head><script language='javascript'>" + "parent.logincallback({'ret':'" + ret
				+ "','msg':'" + msg + "','callBackUrl':'" + callBackUrl+ "'});" + "</script></head> </html>";
		
		response.setContentType("text/html;charset=UTF-8");
		
		try{
			PrintWriter out = response.getWriter();
		    out.print(result);
		    out.flush();
		    out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private String getCallBackUrl(HttpServletRequest request, HttpServletResponse response){
		HttpSession session = request.getSession();
		CookieStore cks = session.getAttribute("cookieStore")==null?
				null:(CookieStore)session.getAttribute("cookieStore");
		
		if (cks == null) {
			return null;
		}
		
		List<org.apache.http.cookie.Cookie> clist = cks.getCookies();
		String callBackUrl = null;
		
		for (org.apache.http.cookie.Cookie ck : clist) {
			if (callBackUrl == null) {
				callBackUrl = this.callBackUrl + "?" + ck.getName() + "=" + ck.getValue();
			} else {
				callBackUrl = callBackUrl + "&" + ck.getName() + "=" + ck.getValue();
			}
		}
		
		if(session.getAttribute("JSESSIONID_CAS")!=null) {
			callBackUrl = callBackUrl + "&JSESSIONID=" + session.getAttribute("JSESSIONID_CAS");
		}
		
		callBackUrl = callBackUrl.replaceAll("CASPRIVACY=", "p2=").replaceAll("TGC=", "p1=").replaceAll("JSESSIONID=", "p3=");
		
		return callBackUrl;
	}
	
	
	public final void setCallBackUrl(String callBackUrl) {
		this.callBackUrl = callBackUrl;
	}
	
}
